//
//  SmallAreaViewController.m
//  WheatPlan
//
//  Created by 郑渊文 on 6/23/15.
//  Copyright (c) 2015 IOSTeam. All rights reserved.
//

#import "SmallAreaViewController.h"
#import "ChooseAreaCell.h"
#import "areaManager.h"

@interface SmallAreaViewController ()
{
    UIButton *backBtn;
    
}
@property(nonatomic,copy)UITableView *myTable;
@property(nonatomic,copy)NSMutableArray *areaNameArray;
@property(nonatomic,copy)NSMutableArray *araeaIdArray;
@end

@implementation SmallAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    [areaManager shareInstance].areaName = @"";
    _areaNameArray = [NSMutableArray array];
    _araeaIdArray = [NSMutableArray array];
    NSDictionary *param = @{@"parentId": _areaId};
    
    [UserDAL getAreasParams: param withBlock:^(NSArray *data, NSError *error) {
        if (data) {
            ////NSLog(@"=========================%@",data);
            
            for (NSDictionary *areaDic in data) {
                [_areaNameArray addObject:areaDic[@"areaName"]] ;
                [_araeaIdArray addObject:areaDic[@"fullPath"]] ;
            }
            if (_areaNameArray.count==0) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
            
            ////NSLog(@"地区名字是%@",_areaNameArray);
            
            [_myTable reloadData];
        }
        else
        {
            
        }
    }];
    
    [self initSubViews];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    
//    self.navigationItem.hidesBackButton =YES;
//    self.navigationItem.leftBarButtonItem.title = @"";
//    
//    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
//                      forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(back)
//     forControlEvents:UIControlEventTouchUpInside];
//    button.frame = CGRectMake(0, 0, 20, 15);
//
//    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
//    
//    self.navigationItem.leftBarButtonItem = menuButton;

    UIBarButtonItem *returnButton = [[UIBarButtonItem alloc]initWithTitle:@"  返回"style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    returnButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = returnButton;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initSubViews
{
    
    self.navigationItem.title = @"选择地区";
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    

    
    self.view.backgroundColor = [UIColor clearColor];
    
    
    //自定义左右按钮
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
//     
//                      forState:UIControlStateNormal];
//    
//    [button addTarget:self action:@selector(back)
//     
//     forControlEvents:UIControlEventTouchUpInside];
//    
//    button.frame = CGRectMake(0, 0, 25, 25);
//    
//    
//    
//    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
//    
//    
//    
//    self.navigationItem.leftBarButtonItem = menuButton;
    
    _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _areaNameArray.count;
}


#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 40;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    ChooseAreaCell *cell = (ChooseAreaCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[ChooseAreaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    
    NSString *areaName = [areaManager shareInstance].areaName;
    if(areaName == nil)
        cell.SelectedimgView.hidden = true;
    else
    {
        if([areaName isEqualToString:[_areaNameArray objectAtIndex:indexPath.row]]){
            cell.SelectedimgView.hidden = false;
            cell.areaLable.textColor = [UIColor orangeColor];}
        else{
            cell.SelectedimgView.hidden = true;
            cell.areaLable.textColor = [UIColor blackColor];
        }
    }
    cell.textLabel.text = _areaNameArray[indexPath.row];
    
    
    return cell;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [areaManager shareInstance].areaName = [_areaNameArray objectAtIndex:indexPath.row];
    [areaManager shareInstance].areaId = [_araeaIdArray objectAtIndex:indexPath.row];
    ////NSLog(@"传了个什么值回去呢%@",[areaManager shareInstance].areaId);
    
    if (self.myareablock!=nil) {
        self.myareablock(_araeaIdArray[indexPath.row]);
    }

    
    [_myTable reloadData];
    [self.navigationController popToRootViewControllerAnimated:YES];

}



-(void)back
{
    [areaManager shareInstance].areaId =nil;
    //NSLog(@"%@",[areaManager shareInstance].areaId);
    [self.navigationController popViewControllerAnimated:YES];
}


@end
